package com.xpgk.common.kit;

import java.util.List;

import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.redis.Redis;

public class CacheKitEx extends CacheKit {
	public static boolean bUseRedis=false;
	
	//是否使用redis,否则使用ehcache
	public static boolean IsUseRedis() {
		return bUseRedis;
	}
	
	public static void UseRedis(boolean useRedis) {
		bUseRedis=useRedis;
	}
	
	public static void put(String cacheName, Object key, Object value) {
		if(bUseRedis)
			Redis.use(cacheName).set(key, value);
		else
			CacheKit.put(cacheName, key, value);
	}
	
	public static <T> T get(String cacheName, Object key) {
		if(bUseRedis)
			return Redis.use(cacheName).get(key);
		else
			return CacheKit.get(cacheName, key);
	}
	
	@SuppressWarnings("rawtypes")
	public static List getKeys(String cacheName) {
		if(bUseRedis)
			return (List)Redis.use(cacheName).keys("*");
		else
			return CacheKit.getKeys(cacheName);
	}
	
	public static void remove(String cacheName, Object key) {
		if(bUseRedis)
			Redis.use(cacheName).del(key);
		else
			CacheKit.remove(cacheName, key);
	}
	
	public static void removeAll(String cacheName) {
		if(bUseRedis)
			Redis.use(cacheName).del("*");
		else
			CacheKit.removeAll(cacheName);
	}
}
